#define GL_SILENCE_DEPRECATION

#import "OpenGLView.h"
#import <OpenGL/gl3.h>


GLuint vertexBuffer;
GLuint program;
GLuint vertexArrayObj;

@implementation OpenGLView

-(id) initWithFrame: (NSRect) frame {
    self = [super initWithFrame: frame];
    running = true;
    // TODO: Init OpenGL

    NSLog(@"initialize");

    return self;
}

// In order to respond to mouse and keyboard events we must be the first responder
-(BOOL) acceptsFirstResponder {
    return YES;
}

-(void) prepareOpenGL {
    [super prepareOpenGL];
    
    glClearColor(0.0, 0.0, 0.0, 1.0);
    
    [self prepareRenderData];
    
    _timer = [NSTimer timerWithTimeInterval:(1.0 / 60.0)
                                   target:self
                                   selector:@selector(visit:)
                                   userInfo:nil
                                   repeats: YES];
    
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}

- (void) clearGLContext {
    glDeleteBuffers(1, &vertexBuffer);
    glDeleteVertexArrays(1, &vertexArrayObj);
    glDeleteProgram(program);
    
    [super clearGLContext];
}

-(void) mouseMoved: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Mouse pos: %lf, %lf", point.x, point.y);
}

-(void) mouseDragged: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Mouse pos: %lf, %lf", point.x, point.y);
}

-(void) scrollWhell: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Mouse whell pos: %lf, %lf", point.x, point.y);
}

-(void) mouseDown: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Left Mouse down: %lf, %lf", point.x, point.y);
}

-(void) mouseUp: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Left Mouse up: %lf, %lf", point.x, point.y);
}

-(void) rightMouseDown: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Right Mouse down: %lf, %lf", point.x, point.y);
}

-(void) rightMouseUp: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Right Mouse up: %lf, %lf", point.x, point.y);
}

-(void) otherMouseDown: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Other Mouse down: %lf, %lf", point.x, point.y);
}

-(void) otherMouseUp: (NSEvent*) event {
    NSPoint point = [self convertPoint: [event locationInWindow] fromView:nil];
    NSLog(@"Other Mouse up: %lf, %lf", point.x, point.y);
}

-(void) mouseEntered: (NSEvent*) event {
    NSLog(@"Mouse entered");
}

-(void) mouseExited: (NSEvent*) event {
    NSLog(@"Mouse left");
}

-(void) keyDown: (NSEvent*) event {
    if ([event isARepeat] == NO) {
        NSLog(@"Key down: %d", [event keyCode]);
    }
}

-(void) keyUp: (NSEvent*) event {
    NSLog(@"Key up: %d", [event keyCode]);
}


-(void) windowDidResize: (NSNotification*) notification {
    NSSize size = [self.window.contentView frame].size;
    NSLog(@"Window resize: %1f, %1f", size.width, size.height);
}

-(void) windowWillClose: (NSNotification*) notification {
    if (running) {
        running = false;

        NSLog(@"Cleanup");
    }

    [NSApp terminate:self];
}

-(void) dealloc {
    [super dealloc];
}

-(BOOL) prepareRenderData {
    return YES;
}

-(void) visit:(NSTimer *)theTimer {
    if ([theTimer isEqual:_timer] == NO) {
        return;;
    }
    
    [self update];
}

- (void)update: (NSTimeInterval) timeInterval {
    
}

- (void) render {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindVertexArray(vertexArrayObj);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(0);
}

@end
